#include "head.h"
/**
 **@author:XuWenqiang
 **@date:2017.7.5
 **@brief:增加addUser和deleteUser函数
 *
*/

void addUser(int sockfd, MSG *pbuf)
{
	MSG msg = *pbuf;
	int sockFd = sockfd;
	
	printf("input id:");
	scanf("%d", &msg.info.id);

	printf("input name:");
	scanf("%s", &msg.info.name);

	printf("input passwd:");
	scanf("%s", &msg.passwd);

	printf("input age:");
	scanf("%d", &msg.info.age);

	printf("input addr:");
	scanf("%s", &msg.info.addr);

	printf("input salary:");
	scanf("%f", &msg.info.salary);

	msg.type = ADMIN_ADD_STAFF;


	char buf[100];
	send(sockFd, &msg, sizeof(msg), 0);
	memset(buf, 0, sizeof(buf));
	recv(sockFd, buf, sizeof(buf), 0);
	printf("recv from server: %s\n", buf);
}

void deleteUser(int sockfd, MSG *pbuf)
{
	
	int sockFd = sockfd;
	MSG msg = *pbuf;
	printf("input name:");
	scanf("%s", &msg.info.name);
	
	msg.type = ADMIN_DEL_STAFF;

	char buf[100];
	send(sockFd, &msg, sizeof(msg), 0);
	memset(buf, 0, sizeof(buf));
	recv(sockFd, buf, sizeof(buf), 0);
	printf("recv from server: %s\n", buf);

}


int do_login(int sockfd, MSG *pbuf)
{
    
}

void do_staff(int sockfd, MSG *pbuf)
{
	int sockFd = sockfd;
	MSG msg = *pbuf;
	while(1)
	{
		printf("*******command menu*************************************\n");
   		printf("*******1: show user 2: change password 3: quit**********\n");
		printf("*******exit*********************************************\n");

		int n;
		char clean[64];
		if (scanf("%d", &n) != 1)
		{
			fgets(clean, 64, stdin);
			printf("\n");
			continue;
		} 

		switch ( n )
		{
/*		case 1 :
			do_show_sample(db);
			break;
		case 2 :
			sqlite3_close(db);
			exit(0);
		default:
			printf("err option\n");
*/			break;
		}
	}
	
}

int do_admin(int sockfd, MSG *pbuf)
{

	int sockFd = sockfd;
	MSG msg = *pbuf;
	while(1)
	{
		printf("******************************************************************\n");    
		printf("1: add user	2: delete user 3: show user 4: change password 5: quit\n");
		printf("******************************************************************\n");
		printf("please select : ");

		int n;
		char clean[64];
		if (scanf("%d", &n) != 1)
		{
			fgets(clean, 64, stdin);
			printf("\n");
			continue;
		} 

		switch ( n )
		{
		case 1 :
			addUser(sockFd, &msg);
			break;
		case 2 :
			deleteUser(sockFd, &msg);
			break;
/*		case 3 :
			do_show_sample(db);
			break;
		case 4 :
			sqlite3_close(db);
			exit(0);
*/		default:
			printf("err option\n");
			break;
		}
	}
	return 0;
}

int main(int argc, char *argv[])
{
    int sockfd, ret;
    struct sockaddr_in servaddr;
    MSG buf;
    char clean[64];

    if (argc < 3)
    {
        printf("Usage : %s <serv_ip> <serv_port>\n", argv[0]);
        exit(-1);
    }

    //创建客户端socket
    if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
    {
        perror("fail to socket");
        exit(-1);
    }

    bzero(&servaddr, sizeof(servaddr));
    servaddr.sin_family = PF_INET;
    servaddr.sin_port = htons(atoi(argv[2]));
    servaddr.sin_addr.s_addr = inet_addr(argv[1]);

    //连接服务器
    if (connect(sockfd, (SA *)&servaddr, sizeof(servaddr)) < 0)
    {
        perror("fail to connect");
        exit(-1);
    }
	printf("connect success!\n");

//    ret = do_login(sockfd, &buf);
//    if(ret == STAFF){  //员工
//        puts("stuff login");
//        do_stuff(sockfd, &buf);
//    }else{  //管理员
        puts("admin login");
        do_admin(sockfd, &buf);
//	}
}
